/*
 * File: Fu_SensAvrg.c
 *
 * Code generated for Simulink model 'Fu_SensAvrg'.
 *
 * Model version                  : 1.75
 * Simulink Coder version         : 8.2 (R2012a) 29-Dec-2011
 * TLC version                    : 8.2 (Dec 29 2011)
 * C/C++ source code generated on : Sat Apr 20 22:18:19 2013
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Fu_SensAvrg.h"
#include "Fu_SensAvrg_private.h"

/* Exported block signals */
uint16_T Adc_RawAcclPdl;               /* '<S2>/Divide' */
uint16_T Adc_RawBrkPdl;                /* '<S2>/Divide3' */
uint16_T Adc_RawVoltU;                 /* '<S2>/Divide5' */
uint16_T Adc_RawVoltV;                 /* '<S2>/Divide6' */
uint16_T Adc_RawVoltW;                 /* '<S2>/Divide7' */
uint16_T Adc_RawVolt48;                /* '<S2>/Divide8' */
uint16_T Adc_RawCurrU;                 /* '<S2>/Divide1' */
uint16_T Adc_RawCurrV;                 /* '<S2>/Divide2' */
uint16_T Adc_RawCurrW;                 /* '<S2>/Divide4' */
uint16_T Adc_RawVoltRefIntern;         /* '<S2>/Divide9' */
uint16_T Net_PercAcclPdl;              /* '<S4>/Divide2' */
uint16_T Net_PercBrkPdl;               /* '<S4>/Divide5' */
int16_T Net_CurrPhasU;                 /* '<S3>/Product2' */
int16_T Net_CurrPhasV;                 /* '<S3>/Product4' */
int16_T Net_CurrPhasW;                 /* '<S3>/Product7' */
int16_T Net_VoltPhasU;                 /* '<S5>/Product2' */
int16_T Net_VoltPhasV;                 /* '<S5>/Product4' */
int16_T Net_VoltPhasW;                 /* '<S5>/Product7' */
int16_T Net_Volt48;                    /* '<S5>/Product9' */

/* Block states (auto storage) */
D_Work_Fu_SensAvrg Fu_SensAvrg_DWork;

/* Real-time model */
RT_MODEL_Fu_SensAvrg Fu_SensAvrg_M_;
RT_MODEL_Fu_SensAvrg *const Fu_SensAvrg_M = &Fu_SensAvrg_M_;

/* Model step function */
void Fu_SensAvrg_step(void)
{
  int32_T tmp;

  /* Product: '<S2>/Divide' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add'
   */
  Adc_RawAcclPdl = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[0] +
    (uint32_T)Nw_AdcRaw[10]) + (uint32_T)Nw_AdcRaw[20]) / 3U);

  /* Product: '<S4>/Divide2' */
  Net_PercAcclPdl = Adc_RawAcclPdl;

  /* Product: '<S2>/Divide3' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add1'
   */
  Adc_RawBrkPdl = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[1] +
    (uint32_T)Nw_AdcRaw[11]) + (uint32_T)Nw_AdcRaw[21]) / 3U);

  /* Product: '<S4>/Divide5' */
  Net_PercBrkPdl = Adc_RawBrkPdl;

  /* Product: '<S2>/Divide5' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add5'
   */
  Adc_RawVoltU = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[5] +
    (uint32_T)Nw_AdcRaw[15]) + (uint32_T)Nw_AdcRaw[25]) / 3U);

  /* Product: '<S5>/Product2' incorporates:
   *  Product: '<S5>/Product'
   */
  tmp = (int16_T)(((int32_T)(4029U * (uint32_T)Adc_RawVoltU >> 16) * 21475 >> 14)
                  * 8833 >> 9) * 16777;
  Net_VoltPhasU = (int16_T)(((tmp < 0 ? 16383 : 0) + tmp) >> 14);

  /* Product: '<S2>/Divide6' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add6'
   */
  Adc_RawVoltV = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[6] +
    (uint32_T)Nw_AdcRaw[16]) + (uint32_T)Nw_AdcRaw[26]) / 3U);

  /* Product: '<S5>/Product4' incorporates:
   *  Product: '<S5>/Product3'
   */
  tmp = (int16_T)(((int32_T)(4029U * (uint32_T)Adc_RawVoltV >> 16) * 21475 >> 14)
                  * 8833 >> 9) * 16777;
  Net_VoltPhasV = (int16_T)(((tmp < 0 ? 16383 : 0) + tmp) >> 14);

  /* Product: '<S2>/Divide7' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add7'
   */
  Adc_RawVoltW = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[7] +
    (uint32_T)Nw_AdcRaw[17]) + (uint32_T)Nw_AdcRaw[27]) / 3U);

  /* Product: '<S5>/Product7' incorporates:
   *  Product: '<S5>/Product5'
   */
  tmp = (int16_T)(((int32_T)(4029U * (uint32_T)Adc_RawVoltW >> 16) * 21475 >> 14)
                  * 8833 >> 9) * 16777;
  Net_VoltPhasW = (int16_T)(((tmp < 0 ? 16383 : 0) + tmp) >> 14);

  /* Product: '<S2>/Divide8' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add8'
   */
  Adc_RawVolt48 = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[8] +
    (uint32_T)Nw_AdcRaw[18]) + (uint32_T)Nw_AdcRaw[28]) / 3U);

  /* Product: '<S5>/Product9' incorporates:
   *  Product: '<S5>/Product8'
   */
  tmp = (int16_T)(((int32_T)(4029U * (uint32_T)Adc_RawVolt48 >> 16) * 21475 >>
                   14) * 8833 >> 9) * 16777;
  Net_Volt48 = (int16_T)(((tmp < 0 ? 16383 : 0) + tmp) >> 14);

  /* Product: '<S2>/Divide1' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add2'
   */
  Adc_RawCurrU = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[2] +
    (uint32_T)Nw_AdcRaw[12]) + (uint32_T)Nw_AdcRaw[22]) / 3U);

  /* Product: '<S2>/Divide2' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add3'
   */
  Adc_RawCurrV = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[3] +
    (uint32_T)Nw_AdcRaw[13]) + (uint32_T)Nw_AdcRaw[23]) / 3U);

  /* Product: '<S2>/Divide4' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add4'
   */
  Adc_RawCurrW = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[4] +
    (uint32_T)Nw_AdcRaw[14]) + (uint32_T)Nw_AdcRaw[24]) / 3U);

  /* Product: '<S2>/Divide9' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add9'
   */
  Adc_RawVoltRefIntern = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRaw[9]
    + (uint32_T)Nw_AdcRaw[19]) + (uint32_T)Nw_AdcRaw[29]) / 3U);
}

/* Model initialize function */
void Fu_SensAvrg_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(Fu_SensAvrg_M, (NULL));

  /* block I/O */

  /* exported global signals */
  Adc_RawAcclPdl = 0U;
  Adc_RawBrkPdl = 0U;
  Adc_RawVoltU = 0U;
  Adc_RawVoltV = 0U;
  Adc_RawVoltW = 0U;
  Adc_RawVolt48 = 0U;
  Adc_RawCurrU = 0U;
  Adc_RawCurrV = 0U;
  Adc_RawCurrW = 0U;
  Adc_RawVoltRefIntern = 0U;
  Net_PercAcclPdl = 0U;
  Net_PercBrkPdl = 0U;
  Net_CurrPhasU = 0;
  Net_CurrPhasV = 0;
  Net_CurrPhasW = 0;
  Net_VoltPhasU = 0;
  Net_VoltPhasV = 0;
  Net_VoltPhasW = 0;
  Net_Volt48 = 0;

  /* states (dwork) */
  (void) memset((void *)&Fu_SensAvrg_DWork, 0,
                sizeof(D_Work_Fu_SensAvrg));

  {
    int32_T tmp;

    /* ConstCode for Product: '<S3>/Product2' */
    tmp = div_s16s32(0, 125) * 3125;
    Net_CurrPhasU = (int16_T)(((tmp < 0 ? 2047 : 0) + tmp) >> 11);

    /* ConstCode for Product: '<S3>/Product4' */
    tmp = div_s16s32(0, 125) * 3125;
    Net_CurrPhasV = (int16_T)(((tmp < 0 ? 2047 : 0) + tmp) >> 11);

    /* ConstCode for Product: '<S3>/Product7' */
    tmp = div_s16s32(0, 125) * 3125;
    Net_CurrPhasW = (int16_T)(((tmp < 0 ? 2047 : 0) + tmp) >> 11);
  }
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
